package com.supermarket_project.service.impl;

import com.supermarket_project.mapper.CashMapper;
import com.supermarket_project.pojo.CashGood;
import com.supermarket_project.pojo.FindGoodName;
import com.supermarket_project.pojo.Goods;
import com.supermarket_project.pojo.Judge;
import com.supermarket_project.service.CashService;
import com.supermarket_project.utils.CreateCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
@Transactional
public class CashServiceImpl implements CashService {

    @Autowired
    private CashMapper cashMapper;

    @Autowired
    private CreateCodeUtil createCodeUtil;


    @Override
    public CashGood find(FindGoodName findGoodName) {
        //查寻good_id,name,price
        CashGood cashGood = cashMapper.findGoodNameByBarCode(findGoodName);
        //判断cashGood是否为空
        if (Objects.isNull(cashGood)){
            return null;
        }
        //存入时间 time
        cashGood.setTime(findGoodName.getTime());
        //根据staffId查询goods_id
        Integer goodId = cashMapper.findGoodId(findGoodName);
        //存入goods_id
        cashGood.setGoodId(goodId);
        //存入staffId
        cashGood.setStaffId(findGoodName.getStaffId());
        //根据根据barCode 查询activity_id
        Integer activityId = cashMapper.findActivityId(findGoodName);
        System.out.println(activityId);
        //根据activity_id查询discount
        Double discount = cashMapper.findDiscount(activityId);
        System.out.println(discount);
        //存入折扣
        cashGood.setDiscount(discount);

//        int i = findGoodName.getQuantity();
//        if(discount<0) {
//            i= (int) (i+(-discount)*i);
//        }
//        //把前端传过来的数量设置到结果对象里面 存入quantity
//        cashGood.setQuantity(i);
        return cashGood;
    }

//    @Override
//    public List<Judge> judge(List<CashGood> list) {
//        ArrayList<Judge> judges = new ArrayList<>();
//        for (int i = 0; i < list.size(); i++) {
//            Judge judge1 = new Judge();
//            CashGood cashGood = list.get(i);
//            //根据barCode查询商品的数量
//            Integer judge = cashMapper.judge(cashGood);
//            if(cashGood.getQuantity()<=judge){
//                judge1.setGoodsId(cashGood.getGoodId());
//                judge1.setJudge(true);
//                judges.add(judge1);
//            }
//            else {
//                judge1.setGoodsId(cashGood.getGoodId());
//                judge1.setJudge(false);
//                judges.add(judge1);
//            }
//        }
//        return judges;
//    }

    @Override
    public List<CashGood> addSerialNumber(List<CashGood> list) {
        String orderId = createCodeUtil.getOrderId();
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setSerialNumber(orderId);
            Integer judge = cashMapper.judge(list.get(i));
            System.out.println(judge);


            if (judge!=null){
                if (judge<=list.get(i).getQuantity()){
                    list.get(i).setBuy("false");
                }
                else {
                    list.get(i).setBuy("true");
                }

            }else{
                list.get(i).setBuy("false");
            }


            Double discount = list.get(i).getDiscount();
            if(discount<0) {
                i= (int) (i+(-discount)*i);
            }
            //把前端传过来的数量设置到结果对象里面 存入quantity
            list.get(i).setQuantity(i);
        }
        return list;
    }


    @Override
    public boolean saveCash(List<CashGood> list) {
        for (int i = 0; i < list.size(); i++) {
            cashMapper.save(list.get(i));
        }
        return true;
    }

    @Override
    public boolean upDateAffirm(String serialNumber) {
        List<CashGood> cashGoodList = cashMapper.queryGoodsBySerialNumber(serialNumber);
        System.out.println(cashGoodList);
        for (CashGood cashGood : cashGoodList) {
            cashMapper.sellGoods(cashGood.getGoodId(),cashGood.getQuantity());
        }
        return cashMapper.upDateAffirm(serialNumber);
    }
}
